#include "Utils.h"
#include "SimpleAudioEngine.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define SOUND_EXT ".ogg"
#else
#define SOUND_EXT ".wav"
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni/JniHelper.h>
#include <jni.h>
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

void Utils::gameServicesSignIn(){
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/flappyreturn/FlappyBirdReturn", "gameServicesSignIn", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
void Utils::updateTopScoreLeaderboard(int score){
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/flappyreturn/FlappyBirdReturn", "updateTopScoreLeaderboard", "(I)V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID, score);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
void Utils::showLeaderboards(){
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/flappyreturn/FlappyBirdReturn", "showLeaderboards", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

void Utils::rateApp(){
	JniMethodInfo methodInfo;
	if (JniHelper::getStaticMethodInfo(methodInfo, "com/nch/flappyreturn/FlappyBirdReturn", "rateApp", "()V"))
	{
		methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
	}
	methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

#endif

void Utils::xcrypt(char* inputByte, unsigned long inputLength, char* encriptedByte, char* key){

	unsigned long keyLength = strlen(key);

	for(int i = 0; i < inputLength; i++){
		int keyIndex = i%keyLength;
		char sourceChar = inputByte[i];

		sourceChar = sourceChar^key[keyIndex];
		encriptedByte[i] = sourceChar;
	}
}

void Utils::playEffect(char* soundName){
	char soundPath[64] = {0};
	sprintf(soundPath, "Sounds/%s%s", soundName, SOUND_EXT);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(soundPath);
}